//
//  HomeViewCell.swift
//  weibo
//
//  Created by 叶子韬韬 on 2017/5/4.
//  Copyright © 2017年 叶子韬韬. All rights reserved.
//

import UIKit
import SDWebImage

internal let edgeMatign :CGFloat = 15
internal let itemMatign :CGFloat = 10

class HomeViewCell: UITableViewCell {
    @IBOutlet weak var iconView: UIImageView!
    
    //控件属性
    @IBOutlet weak var bgViewBottomConstant: NSLayoutConstraint!
    @IBOutlet weak var retweetedLabel: UILabel!
    @IBOutlet weak var picView: PicCollectionView!

    @IBOutlet weak var pictViewWLay: NSLayoutConstraint!
    @IBOutlet weak var pictViewHLay: NSLayoutConstraint!
    @IBOutlet weak var contentLabel: HYLabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var vipLabel: UIImageView!
    @IBOutlet weak var screenNameLabel: UILabel!
    @IBOutlet weak var verifiedView: UIImageView!
    
    //约束属性
    @IBOutlet weak var contentLabelViewLayout: NSLayoutConstraint!

    //加载cell中的数据
    var viewModel : StatuesViewModel?{

       didSet{
        guard let viewModel = viewModel else {
            return
        }
        
        iconView.sd_setImage(with: viewModel.headImageUrl, placeholderImage: UIImage(named: "avatar_default_small"))
        verifiedView.image = viewModel.verifiedImage
        screenNameLabel.text = viewModel.status?.user?.screen_name
        vipLabel.image = viewModel.vipImage
        timeLabel.text = viewModel.createTime
        contentLabel.attributedText = FindEmoticon.shareIntance.findAttrString(statusText: (viewModel.status?.text)!, font: contentLabel.font)
        sourceLabel.text = viewModel.sourceText
        //设置昵称的文字颜色
        screenNameLabel.textColor = viewModel.vipImage == nil ? UIColor.black : UIColor.orange
        
        //计算配图picView的宽度和高度的约束
        let picViewSize = self.calculatePictViewSize(count: viewModel.picURLs.count)
        pictViewWLay.constant = picViewSize.width
        pictViewHLay.constant = picViewSize.height
        
        //将picurl的数据传递给PicCollectionView中的picurl
        picView.picUrl = viewModel.picURLs
        
        //设置转发微博的label
        if viewModel.status?.retweeted_status != nil {
            if let screenName = viewModel.status?.retweeted_status?.user?.screen_name ,let retweenText = viewModel.status?.retweeted_status?.text {
                self.retweetedLabel.text = "@" + "\(screenName) : " + retweenText
            }
            
            //设置转发微博背景显示
            self.bgView.isHidden = false
            
        }else{
            self.retweetedLabel.text = nil
            
            //设置转发微博背景显示
            self.bgView.isHidden = true
        }
        
        
        
       }
    }
    
    
    // MARK:- #注释>   从xib加载cell时会执行
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置微博正文的宽度约束
        contentLabelViewLayout.constant = UIScreen.main.bounds.width-2*edgeMatign
        
        
        
        //监听label点击
        self.contentLabel.userTapHandler = { (label, user, range) in
            print(user)
            print(range)
        }
        
        // 监听链接的点击
        self.contentLabel.linkTapHandler = { (label, link, range) in
            print(link)
            print(range)
        }
        
        // 监听话题的点击
        self.contentLabel.topicTapHandler = { (label, topic, range) in
            print(topic)
            print(range)
        }
        

        
    }



}
// MARK:- #注释>计算约束
extension HomeViewCell{
    
    internal
    func calculatePictViewSize(count:Int) -> CGSize {
        //没有配图
        if count == 0 {
            
            self.bgViewBottomConstant.constant = 0
            return CGSize.zero
        }
        
        //如果有配图需要改变约束
        self.bgViewBottomConstant.constant = 10
        
        
        //取出layout
        let layout = picView.collectionViewLayout as! UICollectionViewFlowLayout
        
        //单张配图
        if count == 1 {
            //请求图片的宽度高度
            //取出图片
            let imag = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: viewModel?.picURLs.first?.absoluteString)
            
            if let image = imag {
                
                //一张图片的collection item.size
                layout.itemSize = CGSize.init(width: image.size.width*2, height: image.size.height*2)
                
                return CGSize.init(width: image.size.width*2, height: image.size.height*2)
                
            }
            
          
        }
        
        
     
        
        //先计算出来 imgeViewWH
        let imageViewWH = (UIScreen.main.bounds.width - 2*edgeMatign - 2*itemMatign)/3
        
        
        
        //多张图片的collection item.size
        layout.itemSize = CGSize.init(width: imageViewWH, height: imageViewWH)
        
        //四张配图
        if count == 4 {
            let picViewWH = imageViewWH*2 + itemMatign
            return CGSize.init(width: picViewWH+1, height: picViewWH)
            
        }
        
        //其他张配图
        //先计算行数
        let rows = CGFloat((count-1) / 3 + 1)
        //计算picView的高度
        let picViewH = (rows * imageViewWH + (rows - 1) * itemMatign)
        
        //计算picView的宽度
        let picViewW = (UIScreen.main.bounds.size.width - 2*edgeMatign)
        
    
        
        return CGSize(width: picViewW, height: picViewH)
    }
}



